﻿using Dapper;
using Dapper.Contrib.Extensions;
using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KWT.DRG_DIP.DB
{
    public class MainDapperHelper
    {

        public static string connString { get; set; }

        /// <summary>
        /// 构造函数注入，方便连接多个数据库
        /// </summary>
        /// <param name="connStr"></param>

        #region 增

        public long Insert<T>(T t) where T : class
        {
            using var conn = new MySqlConnection(connString);
            return conn.Insert(t);
        }

        public async Task<int?> InsertAsync<T>(T t) where T : class
        {
            using var conn = new MySqlConnection(connString);
            return await conn.InsertAsync(t);
        }

        #endregion

        #region 删

        public bool Delete<T>(T t) where T : class
        {
            using var conn = new MySqlConnection(connString);
            return conn.Delete<T>(t);
        }

        public async Task<bool> DeleteAsync<T>(T t) where T : class
        {
            using var conn = new MySqlConnection(connString);
            return await conn.DeleteAsync<T>(t);
        }


        #endregion

        #region 改


        public bool Update<T>(T t) where T : class
        {
            using var conn = new MySqlConnection(connString);
            return conn.Update(t);
        }

        public async Task<bool> UpdateAsync<T>(T t) where T : class
        {
            using var conn = new MySqlConnection(connString);
            return await conn.UpdateAsync(t);
        }

        #endregion

        #region 查
        /// <summary>
        /// 执行查询语句
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql">T-SQL语句</param>
        /// <returns>泛型枚举数</returns>
        public IEnumerable<T> Select<T>(string sql) where T : class
        {
            return Select<T>(sql, null);
        }

        /// <summary>
        /// 执行查询语句
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql">T-SQL语句</param>
        /// <param name="paras">参数</param>
        /// <returns>泛型枚举数</returns>
        public IEnumerable<T> Select<T>(string sql, object paras) where T : class
        {
            using var conn = new MySqlConnection(connString);
            return conn.Query<T>(sql, paras);
        }

        public T SelectSingle<T>(string sql, object paras) where T : class
        {
            using var conn = new MySqlConnection(connString);
            return conn.QuerySingleOrDefault<T>(sql, paras);
        }

        /// <summary>
        /// 执行查询语句(异步)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql">T-SQL语句</param>
        /// <param name="paras">参数</param>
        /// <returns>泛型枚举数</returns>
        public async Task<IEnumerable<T>> SelectAsync<T>(string sql, object paras) where T : class
        {
            using var conn = new MySqlConnection(connString);
            return await conn.QueryAsync<T>(sql, paras);
        }

        public async Task<IEnumerable<T>> SelectAsync<T>(string sql) where T : class
        {
            return await SelectAsync<T>(sql, null);
        }

        public async Task<T> SelectSingleAsync<T>(string sql, object paras) where T : class
        {
            using var conn = new MySqlConnection(connString);
            return await conn.QuerySingleOrDefaultAsync<T>(sql, paras);
        }
        #endregion


        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql">T-SQL语句</param>
        /// <returns>返回行数</returns>
        public int Execute(string sql)
        {
            return Execute(sql, null);
        }

        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql">T-SQL语句</param>
        /// <param name="paras">参数</param>
        /// <returns>返回行数</returns>
        public int Execute(string sql, object paras)
        {
            using var conn = new MySqlConnection(connString);
            return conn.Execute(sql, paras);
        }

        /// <summary>
        /// 执行SQL语句(异步)
        /// </summary>
        /// <param name="sql">T-SQL语句</param>
        /// <param name="paras">参数</param>
        /// <returns>返回行数</returns>
        public async Task<int> ExecuteAsync(string sql, object paras)
        {
            using var conn = new MySqlConnection(connString);
            return await conn.ExecuteAsync(sql, paras);
        }

        public async Task<int> ExecuteAsync(string sql)
        {
            return await ExecuteAsync(sql, null);
        }

        public object ExecuteScalar(string sql)
        {
            return ExecuteScalar(sql, null);
        }

        /// <summary>
        /// 返回第一行第一列的值
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public object ExecuteScalar(string sql, object paras)
        {
            using var conn = new MySqlConnection(connString);
            return conn.ExecuteScalar(sql, paras);
        }

        /// <summary>
        /// 返回第一行第一列的值（异步）
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public async Task<object> ExecuteScalarAsync(string sql, object paras)
        {
            using var conn = new MySqlConnection(connString);
            return await conn.ExecuteScalarAsync(sql, paras);
        }
    }
}
